﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kyers.DataEntity;
using System.Data;
namespace Kyers._50kweb.BaseEntity
{
    public class BaseEntity
    {
        
         static KyersTeamDatabaseContext DBContext {get;set;}
         
        public static KyersTeamDatabaseContext DBModel()
         {
             return DBContext;
        }
        public static void StartDB(string connection)
         {
             if (DBContext == null)
                 DBContext = new KyersTeamDatabaseContext(connection);
             DBContext.Connect();
        }

        /// <summary>
        /// Insert
        /// </summary>
        /// <param name="Entity"></param>
        /// <param name="Table"></param>
        /// <returns></returns>
        public static  int InsertObject(object Entity, string Table)
        {
            return DBContext.insertObject(Entity, Table);
        }

        /// <summary>
        /// Update data
        /// </summary>
        /// <param name="?"></param>
        /// <param name="Table"></param>
        /// <returns></returns>
        public static  int UpdateObject(object Entity, string Table)
        {
            int id = int.Parse(Entity.GetType().GetProperty("id").GetValue(Entity, null).ToString());
            return DBContext.UpdateOnObject(Entity, Table, " id=" + id);
        }

        /// <summary>
        /// Delete 
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Table"></param>
        public static  void DeleteObject(int Id, string Table)
        {
            DBContext.deleteObject(Table, " id=" + Id);
        }

        /// <summary>
        /// Update with custom condition
        /// </summary>
        /// <param name="Entity"></param>
        /// <param name="Table"></param>
        /// <param name="Condition"></param>
        /// <returns></returns>
        public static  int UpdateCustomObject(object Entity, string Table, string Condition = "")
        {
            return DBContext.UpdateOnObject(Entity, Table, Condition);
        }

        /// <summary>
        /// Get All Data
        /// </summary>
        /// <param name="Table"></param>
        /// <returns></returns>
        public static  DataTable GetAllObject(string Table)
        {
            return DBContext.SelectObjects(Table);
        }

        /// <summary>
        /// Get By ID
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Table"></param>
        /// <returns></returns>
        public static  DataTable GetObjectByID(int Id, string Table)
        {
            return DBContext.SelectObjects(Table, " id=" + Id);
        }

        /// <summary>
        /// Get By GuidID
        /// </summary>
        /// <param name="GuidID"></param>
        /// <param name="Table"></param>
        /// <returns></returns>
        public static  DataTable GetObjectByGuidID(Guid GuidID, string Table)
        {
            return DBContext.SelectObjects(Table, " guidid='" + GuidID.ToString() + "'");
        }
        /// <summary>
        /// Get By Field Name
        /// </summary>
        /// <param name="FieldName"></param>
        /// <param name="FieldNameValue"></param>
        /// <param name="Table"></param>
        /// <returns></returns>
        public static  DataTable GetObjectByFieldName(string FieldName, object FieldNameValue, string Table)
        {
            string where="";
            if(FieldNameValue.GetType()==typeof(int))
            {
                where+= FieldName +"="+int.Parse(FieldNameValue.ToString());
            }
            if (FieldNameValue.GetType() == typeof(string))
            {
                where += FieldName + "='" + FieldNameValue.ToString()+"'";
            }
            if (FieldNameValue.GetType() == typeof(decimal))
            {
                where += FieldName + "=" + decimal.Parse(FieldNameValue.ToString());
            }
            if (FieldNameValue.GetType() == typeof(double))
            {
                where += FieldName + "=" + double.Parse(FieldNameValue.ToString());
            }
            if (FieldNameValue.GetType() == typeof(DateTime))
            {
                where += FieldName + "='" + DateTime.Parse(FieldNameValue.ToString()) + "'";
            }
            if (FieldNameValue.GetType() == typeof(float))
            {
                where += FieldName + "=" + float.Parse(FieldNameValue.ToString());
            }
            return DBContext.SelectObjects(Table, where);
        }

        /// <summary>
        /// Get by custom sql command
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static  DataTable GetObjectByCustomSQL(string sql)
        {
            return DBContext.selectObjectsBySql(sql);
        }

        #region Table

        public  const string UserTable = "userdata";

        #endregion
    }
}
